import os
import logging.handlers
import time


logger = logging.getLogger()  # 不加名称设置root logger
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s: - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S')

root_path = os.path.dirname(__file__)
file_name = os.path.join(root_path, "log/log.txt")

# 使用FileHandler输出到文件
rf_handler = logging.handlers.TimedRotatingFileHandler(
    filename=file_name, when='midnight', interval=1, backupCount=180, encoding="utf-8")
rf_handler.setLevel(logging.DEBUG)
rf_handler.setFormatter(formatter)

# # 使用StreamHandler输出到屏幕
# ch = logging.StreamHandler()
# ch.setLevel(logging.DEBUG)
# ch.setFormatter(formatter)

# 添加两个Handler
# logger.addHandler(ch)
logger.addHandler(rf_handler)